package com.example.beacondemo;

import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.os.Parcelable;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Toast;

import org.altbeacon.beacon.Beacon;
import org.altbeacon.beacon.BeaconConsumer;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.Identifier;
import org.altbeacon.beacon.RangeNotifier;
import org.altbeacon.beacon.Region;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class BeaconService extends Service implements BeaconConsumer, RangeNotifier {
    //单位：毫秒
    //后台扫描
    private static final long DEFAULT_BACKGROUND_SCAN_PERIOD = 1000L;
    private static final long DEFAULT_BACKGROUND_BETWEEN_SCAN_PERIOD = 0L;

    //前台扫描
    private static final long DEFAULT_SCAN_PERIOD = 1000L;
    private static final long DEFAULT_BETWEEN_SCAN_PERIOD = 500L;

    private BeaconManager beaconManager;
    private MyBR myBR;
    private BluetoothAdapter bluetoothAdapter;

    public static String bytesToHexString(byte[] src){
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
        //02 01 1a 1a ff 4c 00 02 15
        //384000008cf011bdb23e70b96e4ef00d
        // 2718
        // 6bb9
        // bf
    }

    @Override
    public void onCreate() {
        super.onCreate();

/*        */
        beaconManager = BeaconManager.getInstanceForApplication(BeaconService.this);
        initBeacon();
        beaconManager.bind(this);
        Log.e("BeaconLayout",beaconManager.getBeaconParsers().get(0).getLayout()+"");

        myBR = new MyBR();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("");
        registerReceiver(myBR,intentFilter);
    }

    private void initBeacon() {
        beaconManager.setForegroundScanPeriod(DEFAULT_SCAN_PERIOD);
        beaconManager.setForegroundBetweenScanPeriod(DEFAULT_BETWEEN_SCAN_PERIOD);
        beaconManager.setBackgroundScanPeriod(DEFAULT_BACKGROUND_SCAN_PERIOD);
        beaconManager.setBackgroundBetweenScanPeriod(DEFAULT_BACKGROUND_BETWEEN_SCAN_PERIOD);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(beaconManager != null){
            beaconManager.removeRangeNotifier(this);
        }
        if(myBR!=null){
            unregisterReceiver(myBR);
            Toast.makeText(this, "myBR unregist", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onBeaconServiceConnect() {
        Region region = new Region("all-region-beacon", null,null,null);
        beaconManager.addRangeNotifier(this);
        try {
            beaconManager.startRangingBeaconsInRegion(region);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void didRangeBeaconsInRegion(Collection<Beacon> collection, Region region) {
        Log.e("didRangeBeaconsInRegion",collection.size()+".......");
        List<Beacon> beacons = new ArrayList<>(collection);

        Intent intent = new Intent(MainActivity.BEACON_ACTION);
        intent.putParcelableArrayListExtra("beacon",(ArrayList<? extends Parcelable>)beacons);
        sendBroadcast(intent);
    }

    class MyBR extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            stopSelf();
        }
    }

}
